//
//  SEGHLBNeighbourhoodListViewModel.m
//  SEGViewModel
//
//  Created by 梁学彰 on 2020/11/17.
//

#import "SEGHLBNeighbourhoodListViewModel.h"

@implementation SEGHLBNeighbourhoodListViewModel
#pragma mark - life cycle


#pragma mark - init

#pragma mark - send request
- (RACSignal *)requsetData {
    @weakify(self);
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        NSString *cityID = [SEGProfileManager sharedManager].profile.cityId;
        [self.sharedNewManager requestNeighborhoodWithScopeId:self.scopeType objId:nil cityId:cityID objType:nil pageNo:[NSString stringWithFormat:@"%ld",(unsigned long)self.pageNo] pageLimit:@"10" communityId:kAccountCommunityID moduleId:@"2" finished:^(BOOL isSuccessed, NSString *codeType, NSString *errorMessage) {
            [subscriber sendNext:nil];
            if (!isSuccessed) {
                [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:100 userInfo:@{NSLocalizedDescriptionKey:errorMessage}]];
                return;
            }
            [subscriber sendCompleted];
        }];
        
        
        return nil;
    }];
    return signal;
}

- (RACSignal *)hlb_cellPraiseButtonActionWithIndexPath:(NSIndexPath *)indexPath {
    @weakify(self);
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        
        SEGSharedNeihborhoodInfoModel *model = self.sharedNewManager.neighborhoodListArray[indexPath.row].model;
        SEGSharedCommonUserModel *userModel = self.sharedNewManager.neighborhoodListArray[indexPath.row].user;
        SEGNeighbourhoodOperationType type = [model.operationType integerValue];
        NSString *objType;
        //1：转发PGC、2：求助、3：集市、5：话题
        switch (type) {
            case SEGNeighbourhoodOperationType_FowardPGC:
            {
                objType = @"2";
                break;
            }
            case SEGNeighbourhoodOperationType_Market:
            {
                objType = model.objType;
                break;
            }
            case SEGNeighbourhoodOperationType_Help:
            case SEGNeighbourhoodOperationType_Topic:
            {
                objType = @"2";
                break;
            }
            default:
                break;
        }
        __weak typeof(self) weakSelf = self;
        // 防止重复点赞
        if (self.isLiking) {
            [subscriber sendError:nil];
            return nil;
        }
        self.isLiking = YES;
        if ([model.isPraise isEqualToString:@"1"]) {
            //这里取消点赞行为方法
            [[SEGPraiseorcollectManager sharedManager] requestCancelPraiseByobjId:model.objId objType:objType userId:kAccountUserID type:@"1" finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                if (isSuccessed) {
                    model.isPraise = @"0";
                    model.praiseCount = [NSString stringWithFormat:@"%d",[model.praiseCount intValue] - 1];
                    [subscriber sendCompleted];
                } else {
                    [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:100 userInfo:@{NSLocalizedDescriptionKey:errorMessage}]];
                }
                weakSelf.isLiking = NO;
            }];
        } else {
            //这里执行点赞行为方法
            [[SEGPraiseorcollectManager sharedManager] requestPraiseAttentByObjId:model.objId
                                                                          ObjType:objType
                                                                  objPicUrlForMsg:model.picture.count > 0 ? model.picture[0] : nil
                                                                   objTitleForMsg:model.content
                                                                   byReviewUserId:userModel.userId
                                                                      parentObjId:model.objId
                                                                    parentObjType:objType
                                                                    operationType:model.operationType
                                                                         isParent:@"1" finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                                                                             if (isSuccessed) {
                                                                                 model.isPraise = @"1";
                                                                                 model.praiseCount = [NSString stringWithFormat:@"%d",[model.praiseCount intValue] + 1];
                                                                                 // 重新布局
                                                                                 [subscriber sendCompleted];
                                                                             } else {
                                                                                 [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:100 userInfo:@{NSLocalizedDescriptionKey:errorMessage}]];
                                                                             }
                                                                             weakSelf.isLiking = NO;
                                                                         }];
        }
        return nil;
    }];
    return signal;
}

- (RACSignal *)requesPublishScopeList {
    @weakify(self);
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        WeakSelf;
        [self.socialManager requesPublishScopeListFinished:^(BOOL isSuccessed, NSString* codeType, NSString* errorMessage) {
            if (!isSuccessed) {
                [subscriber sendError:nil];
                return;
            }
            [subscriber sendCompleted];
            
        }];
        return nil;
    }];
    return signal;
}

- (RACSignal *)requestPublishList {
    @weakify(self);
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        if (self.socialManager.dataSourceMarketTypeList.count == 0) {
            [self.socialManager requestFindDictionaryInfoByCode:@"ugc_type" Finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                if(isSuccessed) {
                    [subscriber sendCompleted];
                }else {
                    [subscriber sendError:nil];
                }
            }];
        } else {
            [subscriber sendCompleted];
        }
        
        
        return nil;
    }];
    return signal;
}

- (RACSignal *)refreshListViewWithIndex:(NSIndexPath *)indexPath objId:(NSString *)objId objType:(NSString *)objType isDelete:(BOOL)isDelete {
    @weakify(self);
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        
        if (indexPath.row > self.sharedNewManager.neighborhoodListArray.count-1)  {
            [subscriber sendError:nil];
            return nil;
        }
        if (isDelete == YES) { // 删除
            [self.sharedNewManager.neighborhoodListArray removeObjectAtIndex:indexPath.row];
            [subscriber sendNext:nil];
            return nil;
        }
        // 刷新单条邻里圈数据
        WeakSelf;
        [self.sharedNewManager queryNeighborhoodSingleWithObjId:objId objType:objType finished:^(BOOL isSuccessed, NSString *codeType, NSString *errorMessage) {
            if (!isSuccessed) {
                [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:100 userInfo:@{NSLocalizedDescriptionKey:errorMessage}]];
                return;
            }
            // 刷新单个单元格
            weakSelf.sharedNewManager.neighborhoodListArray[indexPath.row] = weakSelf.sharedNewManager.replaceModel;
            [subscriber sendCompleted];
        }];
        
        
        return nil;
    }];
    return signal;
}

#pragma mark - custom delegate

#pragma mark - system delegate

#pragma mark - event response

#pragma mark - private methods

#pragma mark - setter and getter
- (SEGSharedNewManager *)sharedNewManager {
    if (!_sharedNewManager) _sharedNewManager = [[SEGSharedNewManager alloc] init];
    return _sharedNewManager;
}
- (SEGCommunitySocialManager *)socialManager {
    if (!_socialManager) _socialManager = [[SEGCommunitySocialManager alloc] init];
    return _socialManager;
}
@end
